// 导入用到的包
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import type { RootState, RootThunkAction } from '@/types/store';

// 创建 useInitState 函数（自定义 hook）
// 该 hook 用于在组件挂载时，触发 action，获取初始 state
export const useInitState = <K extends keyof RootState,T extends keyof RootState[K]>(action: RootThunkAction, moduleName: K, stateName: T) => {
  const dispatch = useDispatch();
  const state = useSelector((state: RootState) => state[moduleName][stateName]);

  useEffect(() => {
    dispatch(action());
  }, [dispatch, action]);

  return state;
};